package com.epam.edu.command;

import com.epam.edu.constant.Language;
import com.epam.edu.dao.DAOAdmission;
import com.epam.edu.dao.DAOSpecialty;
import com.epam.edu.dao.DAOTeachingForm;
import com.epam.edu.dao.DAOTeachingPlan;
import com.epam.edu.db.ConnectionPool;
import com.epam.edu.entity.Admission;
import com.epam.edu.entity.Specialty;
import com.epam.edu.entity.TeachingForm;
import com.epam.edu.entity.TeachingPlan;
import com.epam.edu.util.Parser;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;

public class SaveAdmissionsCommand implements Command {
    @Override
    public String perform(HttpServletRequest request, HttpServletResponse response) throws IOException, SQLException, NoSuchAlgorithmException {
        ConnectionPool pool = null;
        Connection connection = null;
        try {
            ConnectionPool.init();
            pool = ConnectionPool.getInstance();
            connection = pool.takeConnection();
            addAdmission(connection, request);
            updateInputMark(connection, request);
            deleteCheckInputMark(connection, request);
            List<TeachingForm> teachingForms = DAOTeachingForm.selectAll(connection);
            request.setAttribute("teachingForms", teachingForms);
            List<Specialty> specialties = DAOSpecialty.selectAll(connection);
            request.setAttribute("specialties", specialties);
            List<TeachingPlan> teachingPlans = DAOTeachingPlan.selectAll(connection);
            request.setAttribute("teachingPlans", teachingPlans);
            List<Admission> admissions = DAOAdmission.selectAll(connection);
            request.setAttribute("admissions", admissions);
            List<String> languages = new LinkedList<String>();
            for (Language lang : Language.values()) {
                languages.add(lang.toString());
            }
            request.setAttribute("languages", languages);
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "/WEB-INF/jsp/admin/catalogAdmissions.jsp";
    }

    private void addAdmission(Connection connection, HttpServletRequest request) {
        String newSpecialty = request.getParameter("newSpecialty");
        if ("-".equals(newSpecialty)) return;
        Specialty specialty = DAOSpecialty.selectByCode(connection, Parser.dividedIntoTwoParts(newSpecialty,'-')[0]);
        String group = request.getParameter("newGroup");
        if ("-".equals(group)) return;
        Language language = Language.valueOf(request.getParameter("newLanguage"));
        TeachingForm teachingForm = DAOTeachingForm.selectByName(connection, request.getParameter("newTeachingForm"));
        TeachingPlan teachingPlan = DAOTeachingPlan.selectByPlanName(connection, request.getParameter("newTeachingPlan"));
        int newPlanCount = Parser.stringToIntParser(request.getParameter("newPlanCount"));
        int id = DAOAdmission.add(connection, new Admission(0, specialty, group, language,
                teachingForm, teachingPlan, newPlanCount, 0, 0));
    }

    private void deleteCheckInputMark(Connection connection, HttpServletRequest request) {
        String[] checkboxes = request.getParameterValues("checkbox");
        if (checkboxes != null) {
            for (String s : checkboxes) {
                int id = Integer.parseInt(s);
                Admission admission = DAOAdmission.selectById(connection, id);
                if (admission != null) {
                    DAOAdmission.delete(connection, admission);
                }
            }
        }
    }

    private void updateInputMark(Connection connection, HttpServletRequest request) {
        String[] admissionId = request.getParameterValues("idAdmission");
        String[] specialties = request.getParameterValues("changeSpecialty");
        String[] groups = request.getParameterValues("changeGroup");
        String[] languages = request.getParameterValues("changeLanguage");
        String[] teachingForms = request.getParameterValues("changeTeachingForm");
        String[] teachingPlans = request.getParameterValues("changeTeachingPlan");
        String[] planCounts = request.getParameterValues("changePlanCount");
        int ih = admissionId.length;
        for (int i = 0; i < admissionId.length; i++) {
            Admission admission = DAOAdmission.selectById(connection, Integer.parseInt(admissionId[i]));
            admission.setSpecialty(DAOSpecialty.selectByCode(connection, specialties[i]));
            admission.setGroup(groups[i]);
            admission.setLanguage(Language.valueOf(languages[i]));
            admission.setTeachingForm(DAOTeachingForm.selectByName(connection, teachingForms[i]));
            admission.setTeachingPlan(DAOTeachingPlan.selectByPlanName(connection, teachingPlans[i]));
            admission.setPlanCount(Parser.stringToIntParser(planCounts[i]));
            DAOAdmission.update(connection, admission);
        }
    }
}
